package com.chaodev.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.CompanyDao;
import com.chaodev.base.model.CompanyInfo;
import com.chaodev.base.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/24 17:24
 **/
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    CompanyDao companyDao;

    @Override
    public CompanyInfo findDataById(String id) {
        return companyDao.findDataById(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveData(CompanyInfo companyInfo) throws DataAccessException {
        CompanyInfo dataById = companyDao.findDataById(companyInfo.getCompanyNo());
        if(ObjectUtil.isNotNull(dataById)){
            companyDao.updateDataById(companyInfo);
        }else{
            companyDao.insertData(companyInfo);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertData(CompanyInfo companyInfo) throws DataAccessException {
        companyDao.insertData(companyInfo);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateDataById(CompanyInfo companyInfo) throws DataAccessException {
        companyDao.updateDataById(companyInfo);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDataById(String id) throws DataAccessException {
        companyDao.deleteDataById(id);
    }

    @Override
    public List<CompanyInfo> queryDataList(CompanyInfo companyInfo) {
        return companyDao.queryDataList(companyInfo);
    }

    @Override
    public PageInfo<CompanyInfo> queryPageList(int start, int limit, CompanyInfo companyInfo) {
        PageHelper.startPage(start, limit);
        List<CompanyInfo> tbCompanyInfoList = companyDao.queryDataList(companyInfo);
        PageInfo<CompanyInfo> pageInfo = new PageInfo<CompanyInfo>(tbCompanyInfoList, limit);
        return pageInfo;
    }
}
